package com.tinem.platform.web.gateway.filter;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import com.tinem.platform.module.pojo.co.GatewayHeadName;
import com.tinem.platform.module.pojo.vo.error.ParamsException;
import com.tinem.platform.module.starter.sdk.MessageCode;
import org.slf4j.MDC;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className DiscoveryRewritePathGatewayFilterFactory
 * @description TODO
 * @site 
 * @company 
 * @create 2020-06-23 18:03
 */
@Component
public class ParamsGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {

    @Override
    public GatewayFilter apply(Object config) {
        return (exchange,chain) -> {
            MDC.clear();
            final ServerHttpRequest request = exchange.getRequest();

            if(!request.getQueryParams().containsKey(GatewayHeadName.X_PLATFORM_GATEWAY_URL_TIMESTAMP)){
                throw new ParamsException(MessageCode.ERROR_COMMONS_PARAMS,GatewayHeadName.X_PLATFORM_GATEWAY_URL_TIMESTAMP);
            }
            if(!NumberUtil.isLong(request.getQueryParams().getFirst(GatewayHeadName.X_PLATFORM_GATEWAY_URL_TIMESTAMP))){
                throw new ParamsException(MessageCode.ERROR_COMMONS_PARAMS,GatewayHeadName.X_PLATFORM_GATEWAY_URL_TIMESTAMP);
            }

            if(!request.getHeaders().containsKey(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_METHOD)){
                throw new ParamsException(MessageCode.ERROR_COMMONS_PARAMS, GatewayHeadName.X_PLATFORM_GATEWAY_REQ_METHOD);
            }
            if(!request.getHeaders().containsKey(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_VERSION)){
                throw new ParamsException(MessageCode.ERROR_COMMONS_PARAMS, GatewayHeadName.X_PLATFORM_GATEWAY_REQ_VERSION);
            }
            if(!request.getHeaders().containsKey(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_API_REQUEST_ID)){
                throw new ParamsException(MessageCode.ERROR_COMMONS_PARAMS, GatewayHeadName.X_PLATFORM_GATEWAY_REQ_API_REQUEST_ID);
            }
            if(!request.getHeaders().containsKey(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_CHARSET)){
                throw new ParamsException(MessageCode.ERROR_COMMONS_PARAMS, GatewayHeadName.X_PLATFORM_GATEWAY_REQ_CHARSET);
            }
            if(!request.getHeaders().containsKey(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_LANG)){
                throw new ParamsException(MessageCode.ERROR_COMMONS_PARAMS, GatewayHeadName.X_PLATFORM_GATEWAY_REQ_LANG);
            }
            //生成网关流水号
            String gateway_request_id = UUID.randomUUID().toString();
            exchange.getAttributes().put(GatewayHeadName.X_PLATFORM_GATEWAY_REQUEST_ID,gateway_request_id);

            exchange.getAttributes().put(GatewayHeadName.X_PLATFORM_GATEWAY_CONTEXT_ACCEPT_TIMESTAMP,System.currentTimeMillis());

            return chain.filter(exchange.mutate().request(exchange.getRequest().mutate()
                    .headers(httpHeaders -> {
                        httpHeaders.put(GatewayHeadName.X_PLATFORM_GATEWAY_REQUEST_ID, CollUtil.newArrayList(gateway_request_id));
                    })
                    .build()).build());
        };
    }
}
